/**
 * org.assess.oa.system.service.impl.TbSystemServiceImpl
 */
package org.assess.oa.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.assess.core.service.BaseServiceImpl;
import org.assess.core.service.ServiceResult;
import org.assess.oa.system.bean.TbSystem;
import org.assess.oa.system.dao.TbSystemDAO;
import org.assess.oa.system.service.TbSystemService;

/**
 * @description 系统信息管理的Service层实现类
 * @author XiaLongxiang
 * @createtime 2012-6-6 上午11:05:22
 */
public class TbSystemServiceImpl extends BaseServiceImpl implements TbSystemService {
    /** 操作系统表的DAO层对象 */
    private TbSystemDAO tbSystemDAO;
    
    /** 日志对象 */
    private static final Logger logger = Logger.getLogger(TbSystemServiceImpl.class);
    
    public TbSystem queryTbSystemByCode(TbSystem system) {
        TbSystem result = null;
        try {
            result = this.tbSystemDAO.queryTbSystemByCode(system.getSystem_code(), system.getValid_flag());
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public List<TbSystem> queryTbSystemAsList(TbSystem system) {
        List<TbSystem> result = null;
        try {
            Map<String, Object> parameter = new HashMap<String, Object>();
            if (system != null) {
                parameter.put("system_name", system.getSystem_name());
                parameter.put("valid_flag", system.getValid_flag());
                parameter.put("used_flag", system.getUsed_flag());
            }
            
            result = this.tbSystemDAO.queryTbSystemAsList(parameter);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public String queryTbSystemAsListString(TbSystem system) {
        String result = "";
        try {
            Map<String, Object> parameter = new HashMap<String, Object>();
            if (system != null) {
                parameter.put("system_name", system.getSystem_name());
                parameter.put("valid_flag", system.getValid_flag());
                parameter.put("used_flag", system.getUsed_flag());
            }
            
            List<TbSystem> records = this.tbSystemDAO.queryTbSystemAsList(parameter);
            result = super.toListPageString(1, -1, records.size(), "getSystem_code", records);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public ServiceResult saveTbSystem(TbSystem system, boolean edit) throws Exception {
        ServiceResult result = ServiceResult.error_unknown;
        try {
            if (StringUtils.isBlank(system.getUsed_flag())) {
                system.setUsed_flag("0");
            }
            
            if (edit) {
                system.setModify_time(new Date());
                system.setModify_user(super.getLoginUserCode());
                this.tbSystemDAO.update(system);
            }
            else {
                system.setCreate_time(new Date());
                system.setCreate_user(super.getLoginUserCode());
                system.setValid_flag("1");
                this.tbSystemDAO.insert(system);
            }
            result = ServiceResult.save_success;
        }
        catch (Exception ex) {
            logger.error(ex);
            throw ex;
        }
        return result;
    }
    
    public String checkTbSystem(TbSystem system) {
        String result = "";
        try {
            TbSystem record = this.tbSystemDAO.queryTbSystemByCode(system.getSystem_code(), "");
            result = super.toCheckExistsString(record != null && StringUtils.isNotBlank(record.getSystem_code()));
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public String updateValid(TbSystem system) throws Exception {
        String result = "";
        try {
            String[] system_codes = system.getSystem_code().split(",");
            system.setModify_time(new Date());
            system.setModify_user(super.getLoginUserCode());
            for (String code : system_codes) {
                system.setSystem_code(code);
                this.tbSystemDAO.updateTbSystemValid(system);
            }
            
            result = super.toResultString(true);
        }
        catch (Exception ex) {
            logger.error(ex);
            throw ex;
        }
        return result;
    }
    
    public void setTbSystemDAO(TbSystemDAO tbSystemDAO) {
        this.tbSystemDAO = tbSystemDAO;
    }
}
